//
//  MBLoginPhoneView.m
//  Meboo
//
//  Created by kyo on 2023/10/7.
//

#import "MBLoginPhoneView.h"

@interface MBLoginPhoneView ()

@property (nonatomic, strong) UIImageView *appleImageView;
@property (nonatomic, strong) UILabel *loginLab;
@property (nonatomic, strong) UIImageView *arrowImageView;

@property (nonatomic, strong) UIImageView *circleImageView;
@property (nonatomic, strong) UIView *circleView;
@property (nonatomic, assign) double angle;

@property (nonatomic, assign) double centerX;
@property (nonatomic, assign) double scale;

@property (nonatomic, assign) BOOL isStop;

@property (nonatomic, assign) BOOL isClick;
@end

@implementation MBLoginPhoneView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        [self setupNotification];
    }
    return self;
}

- (void)setupNotification {
    [MBNotificationCenter addObserver:self selector:@selector(stopAnimaAction) name:LOGIN_SENDED_NOTIFICATION object:nil];
}


#pragma mark - Action
- (void)clickLogin {
    if (self.isClick) {
        return;
    } else {
        self.isClick = YES;
    }
    self.isStop = NO;
    self.arrowImageView.transform = CGAffineTransformMakeScale(1, 1);
    self.circleView.transform = CGAffineTransformMakeScale(0, 0);
    self.arrowImageView.transform = CGAffineTransformMakeTranslation(0, 0);
    if (self.loginBlock) {
        self.loginBlock();
    }
    
    [self arrowMoveStart];
}

- (void)arrowMoveStart {
    self.centerX = 0.0;
    self.scale = 1.0;
    [NSTimer scheduledTimerWithTimeInterval:0.01
                                     target:self
                                   selector:@selector(arrowMoveChanged:)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)arrowMoveChanged:(NSTimer *)timer {
    if (self.isStop || self.centerX == 15) {
        [timer invalidate];
        [self circleStart];
    }
    self.centerX += 0.75;
    self.scale -= 0.05;
    CGAffineTransform translate = CGAffineTransformMakeTranslation(MBWidthScale(self.centerX), 0);
    CGAffineTransform scale = CGAffineTransformScale(translate, self.scale, self.scale);
    self.arrowImageView.transform = scale;
    
    self.circleView.transform = CGAffineTransformMakeScale(1-self.scale, 1-self.scale);
}

- (void)circleStart {
    [NSTimer scheduledTimerWithTimeInterval:0.01
                                     target:self
                                   selector:@selector(progressChanged:)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)progressChanged:(NSTimer *)timer {
    if (self.isStop) {
        [timer invalidate];
    }
    self.angle += 0.05;
    self.circleImageView.transform = CGAffineTransformMakeRotation(self.angle);
}

- (void)stopAnimaAction {
    self.isStop = YES;
    self.isClick = NO;
    self.angle = 0.0;
    self.scale = 1.0;
    self.centerX = 0.0;
    self.arrowImageView.transform = CGAffineTransformMakeScale(1, 1);
    self.circleView.transform = CGAffineTransformMakeScale(0, 0);
    self.arrowImageView.transform = CGAffineTransformMakeTranslation(0, 0);
}

#pragma mark - UI
- (void)setupUI {
    
    UITapGestureRecognizer *actionTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLogin)];
    [self addGestureRecognizer:actionTap];
    
    self.backgroundColor = UIColor.whiteColor;
    self.layer.cornerRadius = MBHeightScale(30);
    self.layer.borderWidth = 1;
    self.layer.borderColor = HEXS(@"22253C").CGColor;
    
    
    [self addSubview:self.appleImageView];
    [self.appleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(25));
        make.width.height.mas_equalTo(MBHeightScale(22));
        make.centerY.equalTo(self);
    }];
    
    [self addSubview:self.loginLab];
    [self.loginLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.appleImageView.mas_right).offset(MBWidthScale(10));
        make.centerY.equalTo(self);
    }];
    
    [self addSubview:self.circleImageView];
    [self.circleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(MBWidthScale(-25));
        make.width.height.mas_equalTo(MBHeightScale(30));
        make.centerY.equalTo(self);
    }];
    
    [self addSubview:self.arrowImageView];
    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.circleImageView);
        make.width.height.mas_equalTo(MBHeightScale(12));
    }];
    
    [self.circleImageView addSubview:self.circleView];
    [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.circleImageView.mas_right);
        make.width.height.mas_equalTo(MBHeightScale(6));
        make.centerY.equalTo(self.circleImageView);
    }];
}

#pragma mark - Setter && Getter
- (UIImageView *)appleImageView {
    if (!_appleImageView) {
        _appleImageView = [UIImageView new];
        _appleImageView.image = [UIImage imageNamed:@"login_phone"];
    }
    return _appleImageView;
}

- (UILabel *)loginLab {
    if (!_loginLab) {
        _loginLab = [UILabel new];
        _loginLab.text = @"Sign in with phone number";
        _loginLab.textColor = HEXS(@"22253C");
        _loginLab.font = MBHelFontSizeScale(17);
    }
    return _loginLab;
}

- (UIImageView *)arrowImageView {
    if (!_arrowImageView) {
        _arrowImageView = [UIImageView new];
        _arrowImageView.image = [UIImage imageNamed:@"login_click_arrow_black"];
    }
    return _arrowImageView;
}

- (UIImageView *)circleImageView {
    if (!_circleImageView) {
        _circleImageView = [UIImageView new];
        _circleImageView.image = [UIImage imageNamed:@"login_login_circle_black"];
    }
    return _circleImageView;
}

- (UIView *)circleView {
    if (!_circleView) {
        _circleView = [UIView new];
        _circleView.layer.cornerRadius = MBHeightScale(3);
        _circleView.backgroundColor = UIColor.whiteColor;
        _circleView.transform = CGAffineTransformMakeScale(0, 0);
    }
    return _circleView;
}

@end
